package com.yh.controller;

import com.yh.service1.api.Dubbo1Service;
import com.yh.service2.api.Dubbo2Service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @AUTHOR: yadong
 * @DATE: 2022/8/1 10:51
 * @DESC:
 */
@RestController
public class ServiceAController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${provider.addr}")
    private String addr;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Reference
    private Dubbo2Service dubbo2Service;
    @Reference
    private Dubbo1Service dubbo1Service;

////    nacos主配置中心注入
//    @Value("${user.name}")
//    private String name;

    //    nacos扩展配置中心注入
    @Value("${db.url}")
    private String aaa;

    //    nacos扩展配置中心注入
    @Value("${age.nn}")
    private String bbb;

    @GetMapping("serviceA")
    public String serviceA(){
        System.out.println(bbb);
       // System.out.println(name);
        System.out.println(aaa);
        System.out.println("ServiceA..........");

        //远程调用
       /* ResponseEntity<String> entity = restTemplate
                .getForEntity(addr + "serviceB/get", String.class);*/

        URI uri = loadBalancerClient.choose("nacos-restful-provider").getUri();
        ResponseEntity<String> entity = restTemplate.getForEntity(uri + "/serviceB/get", String.class);

        String body = entity.getBody();
        return "consumer | "+body;
    }

    @GetMapping("service1")
    public String service1(){

        String res = dubbo1Service.fun();
        return "consumer | "+res;
    }

    @GetMapping("service2")
    public String service2(){
        String res = dubbo2Service.dubboService();
        return "consumer | "+res;
    }
}
